<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="分类图" prop="categoryImage">
        <el-input v-model="dataForm.categoryImage" placeholder="分类图"></el-input>
      </el-form-item>
      <el-form-item label="分类名" prop="categoryName">
        <el-input v-model="dataForm.categoryName" placeholder="分类名"></el-input>
      </el-form-item>
      <el-form-item label="上级分类">
        <el-select v-model="dataForm.parentId" label="上级分类" placeholder="请选择">
          <el-option :value="0" label="请选择"></el-option>
          <el-option
            v-for="categoryListitem in categoryList"
            :value-key="categoryListitem.id"
            :key="categoryListitem.id"
            :label="categoryListitem.categoryName"
            :value="categoryListitem.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否显示" prop="showFlag">
        <el-switch
          v-model="dataForm.showFlag"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="change"
        ></el-switch>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        categoryImage: "",
        categoryName: "",
        parentId: 0,
        showFlag: true,
        sort: 0
      },
      categoryList: [],
      dataRule: {}
    };
  },
  methods: {
    init(id) {
      this.getCategoryList();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.dataForm.parentId = 0;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/category/get/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.status === 200) {
              this.dataForm.categoryImage = data.data.categoryImage;
              this.dataForm.categoryName = data.data.categoryName;
              this.dataForm.parentId = data.data.parentId;
              this.dataForm.showFlag = data.data.showFlag;
              this.dataForm.sort = data.data.sort;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/category/${
                !this.dataForm.id ? "save" : "update/" + this.dataForm.id
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              categoryImage: this.dataForm.categoryImage,
              categoryName: this.dataForm.categoryName,
              parentId: this.dataForm.parentId,
              showFlag: this.dataForm.showFlag,
              sort: this.dataForm.sort
            })
          }).then(({ data }) => {
            if (data && data.status === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.dataForm.parentId = 0;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.message);
            }
          });
        }
      });
    },
    getCategoryList() {
      // 获取商品分类列表，用来给用户选择上一级
      this.$http({
        url: this.$http.adornUrl("/product/category/list"),
        method: "get"
      }).then(({ data }) => {
        if (data && data.status === 200) {
          this.categoryList = data.data.list;
        }
      });
    },
    change() {
      this.$forceUpdate();
    }
  }
};
</script>
